package com.sunny.gateway.config;

import com.alibaba.nacos.common.notify.NotifyCenter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sunny.gateway.filter.SunnyRequestGlobalFilter;

import com.sunny.gateway.handler.GlobalExceptionHandler;
import com.sunny.gateway.handler.MenuPermissionLoader;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 网关配置类
 *
 * @author sunny
 * @date 2025/05/30
 */
@Configuration(proxyBeanMethods = false)
public class GatewayConfiguration {

	@Autowired
	MenuPermissionLoader menuLoader;

	@Autowired
	private  DiscoveryClient discoveryClient;
	/**
	 * 创建PigRequest全局过滤器
	 * @return PigRequest全局过滤器
	 */
	@Bean
	public SunnyRequestGlobalFilter pigRequestGlobalFilter() {
		return new SunnyRequestGlobalFilter();
	}




	/**
	 * 创建全局异常处理程序
	 * @param objectMapper 对象映射器
	 * @return 全局异常处理程序
	 */
	@Bean
	public GlobalExceptionHandler globalExceptionHandler(ObjectMapper objectMapper) {
		return new GlobalExceptionHandler(objectMapper);
	}


	@PostConstruct
	public void post()  {
		//必须要用post 因为bean 加载顺序较晚  顺序 afterpropertiset commandliner PostConstruct
		NotifyCenter.registerSubscriber(menuLoader);
	}
}
